package be.nepherte.movmanager.client.ui.components;

import be.nepherte.movmanager.client.MovManager;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * This class represents an about dialog box which shows the application name
 * and version, authors, copyright and license information. It has a 
 * semi-transparent background effect.
 *
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see http://www.gnu.org/licenses/.
 * </p>
 * 
 * @author	Bart Verhoeven
 * @version	0.1
 */
public class AboutDialogBox extends DialogBox implements ClickHandler {
	/**Content panel.*/
	private final VerticalPanel contentPanel = new VerticalPanel();
	/**Button panel.*/
	private final HorizontalPanel buttonPanel = new HorizontalPanel();
	/**License panel.*/
	private final VerticalPanel licensePanel = new VerticalPanel();
	/**MovManager logo.*/
	private final Image logo = new Image(GWT.getModuleBaseURL() + "images/logo.png");
	/**Label with the name and version of Movmanager.*/
	private final Label title = new Label("MovManager 1.0 Alpha 2");
	/**Label with url to project website.*/
	private final Anchor url = new Anchor("http://code.google.com/p/movmanager/", "http://code.google.com/p/movmanager/", "_blank");
	/**Label with a short description of MovManager.*/
	private final Label description = new Label("An online movie collection manager");
	/**Label with copyright information.*/
	private final HTML copyright = new HTML("Copyright © 2010 Bart Verhoeven<br />Copyright © 2010 Moritz Hoffmann");
	/**GNU General Public License 3.*/
	private final HTML gpl = new HTML();
	/**Close button.*/
	private final Button close = new Button(MovManager.constants.buttonClose(), this);
	/**Exit button.*/
	private final Button exit = new Button(MovManager.constants.buttonClose(), this);
	/**License button.*/
	private final Button license = new Button(MovManager.constants.buttonLicense(), this);
	
	/**
	 * Creates a dialog box showing the about information of this application.
	 */
	public AboutDialogBox() {
		initContentPanel();
		initButtonPanel();
		initDialogBox();
		initLicensePanel();
	}

	/**
	 * Initializes the settings of this dialog box.
	 */
	private void initDialogBox() {
		setAnimationEnabled(true);
		setGlassEnabled(true);
		setPixelSize(400, 300);
		setWidget(contentPanel);
	}

	/**
	 * Initializes the content panel with the actual content.
	 */
	private void initContentPanel() {
		formatContentPanel();
		contentPanel.add(logo);
		contentPanel.add(new HTML("<br />"));
		contentPanel.add(title);
		contentPanel.add(new HTML("<br />"));
		contentPanel.add(description);
		contentPanel.add(new HTML("<br />"));
		contentPanel.add(copyright);
		contentPanel.add(new HTML("<br />"));
		contentPanel.add(url);
		contentPanel.add(new HTML("<br />"));
		contentPanel.add(buttonPanel);
	}
	
	/**
	 * Initializes the button panel with the license and close buttons.
	 */
	private void initButtonPanel() {
		formatButtonPanel();
		buttonPanel.add(license);
		buttonPanel.add(close);
	}
	
	/**
	 * Initializes the license panel with the GNU General Public License 3.
	 */
	private void initLicensePanel() {
		formatLicensePanel();
		gpl.setHTML("MovManager is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later <br /><br />" +
					"MovManager is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <br /><br />" +
					"You should have received a copy of the GNU General Public License along with MovManager. If not, see <a href=\"http://www.gnu.org/licenses/\" target=\"_blank\">http://www.gnu.org/licenses/</a>.");
		licensePanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		licensePanel.add(gpl);
		licensePanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		licensePanel.add(exit);
	}

	/**
	 * Formats the content panel: correct style, size and alignment is applied.
	 */
	private void formatContentPanel() {
		title.addStyleName("headerStyle");
		contentPanel.setWidth("100%");
		contentPanel.setHeight("100%");
		contentPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		contentPanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
	}

	/**
	 * Formats the button panel: correct style, size and alignment is applied.
	 */
	private void formatButtonPanel() {
		buttonPanel.setSpacing(10);
		close.setPixelSize(55, 20);
		license.setPixelSize(60, 20);
	}
	
	/**
	 * Formats the license panel: correct style, size and alignment is applied.
	 */
	private void formatLicensePanel() {
		licensePanel.setSpacing(15);
		licensePanel.setWidth("100%");
		licensePanel.setHeight("100%");
		licensePanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		licensePanel.setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
	}

	/**
	 * Called when a click event is fired by one of the clickable components in
	 * this about dialog bog.
	 */
	@Override
	public void onClick(ClickEvent event) {
		// the close button has been clicked
		if (event.getSource().equals(close)) {
			AboutDialogBox.this.hide();
		}
		
		// the license button has been clicked
		if (event.getSource().equals(license)) {
			setWidget(licensePanel);
		}
		
		// the exit button has been clicked
		if (event.getSource().equals(exit)) {
			setWidget(contentPanel);
		}
	}
}
